package com.lms.controller;

import com.lms.bean.Book;
import com.lms.bean.Type;
import com.lms.service.BookService;
import com.lms.service.TypeService;
import com.lms.util.MyStringUtils;
import com.lms.util.QueryResult;
import com.lms.util.ResultUtils;
import com.lms.vo.BookVO;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("book")
public class BookController {
    @Autowired
    private BookService bookService;

    @Autowired
    private TypeService typeService;

    @RequestMapping("/admin/book-list")
    public String toBookList(Model model) {
        List<Type> typeList = typeService.findAll();
        model.addAttribute("typeList", typeList);
        return "admin/book-list";
    }

    @RequestMapping("/admin/book-input")
    public String toBookInput(Long bookId, Model model) {
        if (-1 == bookId) {
            model.addAttribute("operation", "添加图书");
            model.addAttribute("book", new Book());
        } else {
            model.addAttribute("operation", "更新图书");
            Book book = bookService.findBookById(bookId);
            model.addAttribute("book", book);
        }
        List<Type> types = typeService.findAll();
        model.addAttribute("types", types);
        return "admin/book-input";
    }

    @RequestMapping("save")
    public String save(Book book) {
        bookService.save(book);
        return "redirect:admin/book-list";
    }

    @RequestMapping("delete")
    public String deleteById(Long bookId) {
        bookService.deleteById(bookId);
        return "redirect:admin/book-list";
    }

    @RequestMapping("deleteAll")
    public String deleteById(String ids) {
        bookService.deleteAll(ids);
        return "redirect:admin/book-list";
    }

    /**
     * @param pageFromLayUI  计数方式是 1,2,3,4...,n  默认为 1 ;
     *                       但是 Pageable.page 的计数方式是 0,1,2,3,...,n-1 默认为 0
     * @param limitFromLayUI 等于 Pageable.size
     * @return 能被 layUI 接受的 JSON, 格式为 { "code": 0, "msg": "OK", count": 14, "data":[{},{},{}] }
     */
    @RequestMapping("all")
    @ResponseBody
    public ResultUtils<BookVO> findAll(@RequestParam(value = "page", defaultValue = "1") Integer pageFromLayUI,
                                       @RequestParam(value = "limit", defaultValue = "10") Integer limitFromLayUI,
                                       @RequestParam(value = "queryParam[bookId]", defaultValue = "") String bookId,
                                       @RequestParam(value = "queryParam[keyWord]", defaultValue = "") String keyWord,
                                       @RequestParam(value = "queryParam[typeId]", defaultValue = "-1") String typeId,
                                       @RequestParam(value = "queryParam[queryType]", defaultValue = "1") String queryType) {


        if ("0".equals(queryType)) {
            //精确查询
            if (MyStringUtils.isNumeric(bookId)) {
                //传入的图书编号是数字
                Book book = bookService.findBookById(Long.parseLong(bookId));
                if (null != book) {
                    BookVO bookVO = BookVO.convert(book);
                    List<BookVO> bookVOList = new ArrayList<>();
                    bookVOList.add(bookVO);
                    return ResultUtils.layUITable(1L, bookVOList);
                }
            }
            //传入的图书编号不是数字 或者 null==book
            return ResultUtils.layUITable(0L, new ArrayList<>());

        } else {
            //模糊查询
            Book param = new Book();
            if(null==keyWord){
                keyWord ="";
            }
            String s = StringUtils.trimAllWhitespace(keyWord);
            param.setBookName(s);
            param.setBookDescription(s);
            param.setBookWriter(s);
            QueryResult<BookVO> queryResult;
            if ("-1".equals(typeId) || !MyStringUtils.isNumeric(typeId)) {
                //不限图书类别
                queryResult = bookService.queryByKeyWordLikeNoType(pageFromLayUI - 1, limitFromLayUI, param);
            } else {
                //限制图书类别
                queryResult = bookService.queryByKeyWordLikeWithType(pageFromLayUI - 1, limitFromLayUI, param, Long.parseLong(typeId));
            }
            //返回JSON结果
            return ResultUtils.layUITable(queryResult.getTotalCount(), queryResult.getData());
        }

    }


}
